from PyQt4.QtGui import QWidget, QLabel, QPixmap
from ..widgets.day_and_date import DayAndDate
from ..widgets.title_panel import TitleAndButtons
from ..widgets.stats_panel import StatsPanel
from ..widgets.viewscreen import Viewscreen
from ..widgets.printer import PrinterWidget
from ..widgets.mixed_button import MixedButton
from ..data import game_data, save_manager
from . import screen_manager

class MainScreen(QWidget):
	def __init__(self, *args, **kw):
		QWidget.__init__(self, *args, **kw)
		self.setGeometry(0, 0, 1024, 768)
		self.setObjectName("MainScreen")
#
#		combined day and date widget
#
		self.day_and_date = DayAndDate(self)
		self.day_and_date.setGeometry(1024-414, 20, 394 , 58)
#
#		title panel with forward and back buttons attached
#
		self.title = TitleAndButtons(self)
		self.title.set_title_text("Nameless")
		self.title.setGeometry(120, 12, 456, 52)
#
#		panel to show brothel and player statistics
#
		self.status = StatsPanel(self)
		self.status.setText("")
		self.status.setGeometry(720, 90, 269, 304)
#
#		viewscreen showing city sector
#
		self.screen = Viewscreen(self)
		self.screen.setGeometry(20, 40, 660, 551)
#
#		"printer" widget to display messages to the user
#
		self.printer = PrinterWidget(self)
		self.printer.setGeometry(20, 588, 665, 117)
#
#		there are a ton of buttons to define
#
#		start with the quit button
#
		quit_butt = MixedButton("Quit", self)
		quit_butt.setGeometry(20, 708, 210, 50)
		quit_butt.clicked.connect(
			self.to_main_menu
		)
#
#		load button
#
		self.save_butt = MixedButton("Save", self)
		self.save_butt.setGeometry(260, 708, 210, 50)
#
#		summary/report button
#
		summ_butt = MixedButton("Report", self)
		summ_butt.setGeometry(500, 708, 210, 50)
		#summ_butt.clicked.connect(self.to_main_menu)
#
#		"girls" screen
#
		tart_butt = MixedButton("Tarts", self)
		tart_butt.setGeometry(740, 408, 210, 50)
		tart_butt.clicked.connect(self.to_tarts_screen)
#
#		goons screen
#
		ruff_butt = MixedButton("Ruffians", self)
		ruff_butt.setGeometry(740, 468, 210, 50)
		#ruff_butt.clicked.connect(self.to_main_menu)
#
#		brothel management
#
		brot_butt = MixedButton("Brothel", self)
		brot_butt.setGeometry(740, 528, 210, 50)
		#brot_butt.clicked.connect(self.to_main_menu)
#
#		dungeon screen
#
		dunn_butt = MixedButton("Dungeon", self)
		dunn_butt.setGeometry(740, 588, 210, 50)
		#dunn_butt.clicked.connect(self.to_main_menu)
#
#		Town Screen
#
		town_butt = MixedButton("Town", self)
		town_butt.setGeometry(740, 648, 210, 50)
		town_butt.clicked.connect(self.to_town_screen)
#
#		next turn 
#
		next_butt = MixedButton("Next Turn", self)
		next_butt.setGeometry(740, 708, 210, 50)
		#next_butt.clicked.connect(self.to_main_menu)
#
#		hide the screen initially
#
		self.hide()

#
#	before we show the screen we need to update the
#	data fields
#
	def show(self):
		data = game_data.data
#
#		this isn't set when the class is defined.
#		so we do it when the screen is displayed
#
		self.save_butt.clicked.connect(
			self.save_game
		)
#
#		we'll need the brothel data
#
		brothel_data = data.current_brothel()
		player_data = data.player_data()
		print "showing ", brothel_data.name

		self.title.set_title_text(brothel_data.name)

		status_text = ""
		status_text += "Gold: {}".format(data.gold)
		status_text += "\n"
		status_text += "Standing: " + player_data.rep_str()
		status_text += "\n"
		status_text += "~~~~~~"
		status_text += "\n"
		status_text += "Rooms: " + brothel_data.room_str() 
		status_text += "\n"
		status_text += "Girls: " + brothel_data.girl_str() 
		status_text += "\n"
		status_text += "Customers: " + brothel_data.happy_str() 
		status_text += "\n"
		status_text += "Fame: " + brothel_data.fame_str() 
		status_text += "\n"
		status_text += "Cleanliness: " + brothel_data.dirt_str() 
		status_text += "\n"
		status_text += "Bestiary: " + brothel_data.beast_str() 
		status_text += "\n"
		status_text += "Neither Bar nor Casino"

		self.status.setText(status_text)
#
#		set the day and date
#
		self.day_and_date.set_date()
		#self.day_roller.set_day()
		#self.date_roller.set_date()

#
#		call the QWidget show func to do the actual showing
#
		QWidget.show(self)


	def to_tarts_screen(self):
		screen_manager.show("tarts screen")
	def to_town_screen(self):
		screen_manager.show("town screen")
	def to_main_menu(self):
		screen_manager.show("main menu")

#
#	pick up key pressed events
#
#	this belongs at app level, really
#	but this'll do for testing
#
	def keyPressEvent(self,e):
		n = -1
		k = e.key()
		if k == ord("1"): n = 0
		if k == ord("2"): n = 1
		if k == ord("3"): n = 2
		if k == ord("4"): n = 3
		if k == ord("5"): n = 4
		if k == ord("6"): n = 5
		if n != -1:
			print "choosing: ", n
			e.accept()
			self.screen.choose(n)
			return
		if k == ord("O"):
			e.accept()
			screen_manager.show("town screen")
			return
		if k == ord("G"):
			e.accept()
			screen_manager.show("tarts screen")
			return
		if k == ord("S"):
			e.accept()
			screen_manager.show("slave market screen")
			return
		e.ignore()


	def count(self):
		return len(self.saves)

	def save_game(self):
		data = game_data.data
		save_manager.save_game()
#
#		a bit of chat for the player
#
		s = "Master, your affairs have been saved under the heading '{}'."
		self.printer.set_text(
			s.format(data.brothels[0].name)
		)



